1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <iostream> #include <cstdio> #include <cstring> #include <queue>
using namespace std;
const int MAXN = 10 + 5; const int MAXM = 1 << 10;
const int INF = 0x3f3f3f3f;
const int NextX[4]= {- 1, 0, 0, 1}, NextY[4]= {0, - 1, 1, 0};
int N, M; int Map[MAXN][MAXN]= {0};
struct preSt { int x, y; int state;
preSt (int fx = 0, int fy = 0, int fs = 0) : x (fx), y (fy), state (fs) {} } ;
int f[MAXN][MAXN][MAXM]; preSt pre[MAXN][MAXN][MAXM]; int cnt = 0;
queue<pair<int, int> > que; void SPFA (int state) { while (! que.empty()) { pair<int, int> top = que.front(); que.pop();
int x = top.first, y = top.second; for (int i = 0; i < 4; i ++) { int tx = x + NextX[i]; int ty = y + NextY[i]; if (tx < 1 || tx > N || ty < 1 || ty > M) continue; if (f[x][y][state] + Map[tx][ty] < f[tx][ty][state]) { f[tx][ty][state] = f[x][y][state] + Map[tx][ty]; pre[tx][ty][state] = preSt (x, y, state); que.push(make_pair (tx, ty)); } } } }
int tag[MAXN][MAXN]= {0}; void traceback (int x, int y, int state) { if (! x || ! y) return ; tag[x][y] = 1; preSt pr = pre[x][y][state]; traceback (pr.x, pr.y, pr.state); if (pr.x == x && pr.y == y) traceback (pr.x, pr.y, state - pr.state); }
int getnum () { int num = 0; char ch = getchar ();
while (! isdigit (ch)) ch = getchar (); while (isdigit (ch)) num = (num << 3) + (num << 1) + ch - '0', ch = getchar ();
return num; }
int main () { memset (f, 0x3f, sizeof (f)); N = getnum (), M = getnum (); int px, py; for (int i = 1; i <= N; i ++) for (int j = 1; j <= M; j ++) { Map[i][j] = getnum (); if (! Map[i][j]) { cnt ++, f[i][j][1 << (cnt - 1)] = 0; px = i, py = j; } } int limit = (1 << cnt) - 1; for (int state = 1; state <= limit; state ++) { for (int i = 1; i <= N; i ++) for (int j = 1; j <= M; j ++) { for (int sub = state & (state - 1); sub; sub = (sub - 1) & state) if (f[i][j][sub] + f[i][j][state - sub] - Map[i][j] < f[i][j][state]) { f[i][j][state] = f[i][j][sub] + f[i][j][state - sub] - Map[i][j]; pre[i][j][state] = preSt (i, j, sub); } if (f[i][j][state] < INF) que.push(make_pair (i, j)); } SPFA (state); } traceback (px, py, limit); printf ("%d\n", f[px][py][limit]); for (int i = 1; i <= N; i ++) { for (int j = 1; j <= M; j ++) { if (! Map[i][j]) putchar ('x'); else { tag[i][j] ? putchar ('o') : putchar ('_'); } } puts (""); }
return 0; }
|